package com.thz.websqlit.mybatis.flexigrid;

import com.thz.websqlit.mybatis.annotation.Columns;

import java.io.Serializable;
import java.lang.reflect.Field;

public class PagingBean implements Serializable {
    /**
     * 当前页面
     */
    private Integer page = null;

    /**
     * 容量
     */
    private Integer rp = null;

    /**
     * 数据的开始
     */
    private Integer start = null;

    /**
     * 排序  asc
     */
    private String sortorder = null;

    /**
     * 按某个字段来排序
     */
    private String sortname = null;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRp() {
        return rp;
    }

    public void setRp(Integer rp) {
        this.rp = rp;
    }

    public Integer getStart() {
        return (page-1)*rp;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public String getSortorder() {
        return sortorder;
    }

    public void setSortorder(String sortorder) {
        this.sortorder = sortorder;
    }

    public String getSortname() {
        return sortname;
    }

    public void setSortname(String sortname) {
        this.sortname = sortname;
    }


    @Override
    public String toString() {
        return "PagingBean{" +
                "page=" + page +
                ", rp=" + rp +
                ", start=" + start +
                ", sortorder='" + sortorder + '\'' +
                ", sortname='" + sortname + '\'' +
                '}';
    }

    public void dealSortName(Class<?> classez){
        if(sortname == null){
            return;
        }
        try {
            Field field = classez.getDeclaredField(sortname);
            if(field.isAnnotationPresent(Columns.class)){
                Columns columns = field.getDeclaredAnnotation(Columns.class);
                sortname = columns.value();
            }
        } catch (NoSuchFieldException e) {

        }

    }
}
